package com.thrift.distributed.process;

import lombok.extern.slf4j.Slf4j;

/**
 * @包名称 com.thrift.distributed.process
 * @类描述 <pre></pre>
 * @作者 zhangbo
 * @创建时间 2019/9/21
 * @版本 1.00
 * @修改记录 <pre>
 *     版本           修改人         修改日期         修改内容描述
 *     -----------------------------------------------------------
 *     1.00           Lenovo        2019/9/21
 *     -----------------------------------------------------------
 * </pre>
 */
@Slf4j
public class RegistRunable implements Runnable {

    private ClientListener clientListener;

    /*注册中心超时或重复检测休眠时间*/
    private int zookeeperServiceSleepTime;

    public RegistRunable(ClientListener clientListener,int zookeeperServiceSleepTime) {
        this.clientListener = clientListener;
        this.zookeeperServiceSleepTime = zookeeperServiceSleepTime;
    }

    @Override
    public void run() {
        clientListener.init();
        synchronized (this) {
            while (true) {
                clientListener.updateServiceMap();
                try {
                    Thread.sleep(zookeeperServiceSleepTime);
                } catch (InterruptedException e) {
                    log.error("守护线程休眠异常：{}",e);
                    new Thread(new RegistRunable(clientListener,zookeeperServiceSleepTime)).start();
                    log.info("重新启动新守护线程");
                    Thread.currentThread().interrupt();
                }
            }
        }
    }

}
